#!/bin/bash
 
clear

IMAGELISTFILENAME="imageList"

DIR_WITH_FRAMES="./"
if [ $# -ne 4 ]
then 
  echo "Please provide a path argument to the path that contains the directory with your frames"
  exit 0
else
 DIR_WITH_FRAMES=$1 
fi

echo "Frame Directory $DIR_WITH_FRAMES"

 
STARTDIR=`pwd`
#Switch to this directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$DIR"


mkdir temp
cd temp
TEMPDIR=`pwd` 
rm *.jpg


FILES=$DIR_WITH_FRAMES/*
echo "Detected Files $FILES"
COUNTER=0
for f in $FILES
do
   ISITACOLORFRAME=`echo $f | grep "color"` 
   if [ -n "$ISITACOLORFRAME" ] 
    then
     echo "Processing $f file..."
     convert $f "$TEMPDIR/$COUNTER.jpg"
     COUNTER=$[$COUNTER +1]
     else
     echo "Unknown $f file..."  
    fi

    if [ $COUNTER -gt 80 ] 
     then 
      break
    fi
done

echo "Temp Dir is $TEMPDIR"

cd $TEMPDIR

ls | grep jpg > $IMAGELISTFILENAME

if [ -e calibration ] 
then 
  echo "Refreshing link to calibration executable"
  rm calibration
fi

ln -s $DIR/calibration

./calibration -w $2 -h $3 -s $4 -oe -o colorCalibration $IMAGELISTFILENAME

#../ExtrinsicCalibration/extrinsicCalibration -w 6 -h 9 -s 0.02 -c colorCalibration -i $DIR_WITH_FRAMES/colorFrame_0_00000.pnm

mv colorCalibration $DIR_WITH_FRAMES/color.calib
cp $DIR_WITH_FRAMES/color.calib $DIR_WITH_FRAMES/depth.calib

rm *.jpg

cd $DIR
rmdir temp
 
cd "$STARTDIR"

exit 0
